package com.sirui.x.learnIo.netty;

import com.sirui.x.learnIo.netty.handle.ClientHandleIn1;
import com.sirui.x.learnIo.netty.handle.ClientHandleIn2;
import com.sirui.x.learnIo.netty.handle.ClientHandleOut1;
import com.sirui.x.learnIo.netty.handle.ClientHandleOut2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClientWrapper {

	public static void main(String[] args) throws InterruptedException {

		EventLoopGroup loopGroup = new NioEventLoopGroup();

		Bootstrap bootstrap = new Bootstrap();
		try {
			bootstrap.channel(NioSocketChannel.class).group(loopGroup).remoteAddress("127.0.0.1", 4200)
					.handler(new ChannelInitializer<SocketChannel>() {

						protected void initChannel(SocketChannel ch) throws Exception {
							ChannelPipeline pipeline = ch.pipeline();
							pipeline.addLast(new ClientHandleIn1());
							pipeline.addLast(new ClientHandleIn2());
							pipeline.addLast(new ClientHandleOut2());
							pipeline.addLast(new ClientHandleOut1());
						};
					});
			ChannelFuture future = bootstrap.connect().sync();
			future.channel().closeFuture().sync();
		} finally {
			loopGroup.shutdownGracefully();
		}
	}
}
